package com.yuncheng.oaApi.controller;

import java.util.*;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


import com.yuncheng.oaApi.constant.NkConstant;
import com.yuncheng.oaApi.entity.NkRsSzflkm;
import com.yuncheng.vo.HttpResult;
import com.yuncheng.common.query.QueryBuilder;
import com.yuncheng.annotation.AutoLog;
import com.yuncheng.utils.I18nUtil;
import com.yuncheng.common.base.controller.BaseController;
import com.yuncheng.oaApi.service.INkRsSzflkmService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuncheng.oaApi.vo.NkRsSzFlKmVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.yuncheng.system.api.SystemAPI;
import org.springframework.context.annotation.Lazy;


/**
 * @Description: 内控-收支分类科目
 * @Author: lhb
 * @Date: 2024-02-22
 * @Version: V1.0
 */
@Api(tags = "内控-收支分类科目接口")
@RestController
@RequestMapping("/api/spzx-oa/nkRsSzflkm")
public class NkRsSzflkmController extends BaseController<NkRsSzflkm, INkRsSzflkmService> {

    private static final Logger log = LoggerFactory.getLogger(NkRsSzflkmController.class);

    @Resource
    private INkRsSzflkmService nkRsSzflkmService;

    @Resource
    @Lazy
    private SystemAPI systemAPI;

    /**
     * 分页列表查询
     *
     * @param nkRsSzflkm
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "内控-收支分类科目-分页列表查询")
    @ApiOperation(value = "内控-收支分类科目-分页列表查询")
    @GetMapping(value = "/list")
    public HttpResult<?> queryPageList(NkRsSzflkm nkRsSzflkm, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {
        QueryWrapper<NkRsSzflkm> queryWrapper = QueryBuilder.initQueryWrapper(nkRsSzflkm, req.getParameterMap());
        Page<NkRsSzflkm> page = new Page<>(pageNo, pageSize);
        IPage<NkRsSzflkm> pageList = nkRsSzflkmService.page(page, queryWrapper);
        for(NkRsSzflkm vo:pageList.getRecords()){
            formatValue(vo);
        }
        return HttpResult.ok(pageList);
    }

    @AutoLog(value = "获取收支分类科目列表-部门预算支出经济分类科目")
    @ApiOperation(value = "获取收支分类科目列表-部门预算支出经济分类科目")
    @GetMapping(value = "/getbmyszcjjflkm")
    public HttpResult<?> getbmjjfl(String kmlx) {
    if (kmlx.equals(NkConstant.SZFLKM_KMLX_BM)){
        List<NkRsSzflkm> bmlist = nkRsSzflkmService.list(new QueryWrapper<NkRsSzflkm>().eq("kmlx", NkConstant.SZFLKM_KMLX_BM));

        List<NkRsSzFlKmVo> nkRsSzFlKmVos = new ArrayList();

        bmlist.forEach( bean -> {
            NkRsSzFlKmVo vo = new NkRsSzFlKmVo();
            BeanUtils.copyProperties(bean, vo);
            nkRsSzFlKmVos.add(vo);
        });
        for (NkRsSzFlKmVo nkRsSzFlKm :nkRsSzFlKmVos){
            List<NkRsSzFlKmVo> collect = nkRsSzFlKmVos.stream().filter(f -> nkRsSzFlKm.getKmdm().equals(f.getZkmdm())).collect(Collectors.toList());
            nkRsSzFlKm.setChildren(collect);
        }

        List<NkRsSzFlKmVo> collect = nkRsSzFlKmVos.stream().filter(f -> f.getZkmdm() == null).collect(Collectors.toList());
        Map map = new HashMap();
        map.put("records",collect);
        return HttpResult.ok(map);
    }else if (kmlx.equals(NkConstant.SZFLKM_KMLX_ZF)){
        List<NkRsSzflkm> zflist = nkRsSzflkmService.list(new QueryWrapper<NkRsSzflkm>().eq("kmlx", NkConstant.SZFLKM_KMLX_ZF));

        List<NkRsSzFlKmVo> nkRsSzFlKmVos = new ArrayList();

        zflist.forEach(
                bean -> {
                    NkRsSzFlKmVo vo = new NkRsSzFlKmVo();
                    BeanUtils.copyProperties(bean, vo);
                    nkRsSzFlKmVos.add(vo);
        });
        for (NkRsSzFlKmVo nkRsSzFlKm :nkRsSzFlKmVos){
            List<NkRsSzFlKmVo> collect = nkRsSzFlKmVos.stream().filter(f -> nkRsSzFlKm.getKmdm().equals(f.getZkmdm())).collect(Collectors.toList());
            nkRsSzFlKm.setChildren(collect);
        }
        List<NkRsSzFlKmVo> collect = nkRsSzFlKmVos.stream().filter(f -> f.getZkmdm() == null).collect(Collectors.toList());
        Map map = new HashMap();
        map.put("records",collect);
        return HttpResult.ok(map);
    }

    return HttpResult.ok();
    }
    /**
     * 添加
     *
     * @param nkRsSzflkm
     * @return
     */
    @AutoLog(value = "内控-收支分类科目-添加")
    @ApiOperation(value = "内控-收支分类科目-添加")
    //@RequiresPermissions("nk:NkRsSzflkm:add")
    @PostMapping(value = "/add")
    public HttpResult<?> add(@RequestBody NkRsSzflkm nkRsSzflkm) {
        HttpResult<String> result = new HttpResult(true);
        nkRsSzflkmService.save(nkRsSzflkm);
        result.setResult(nkRsSzflkm.getId());
        result.setMessage(I18nUtil.message("save.success"));
        return result;
    }

    /**
     * 编辑
     *
     * @param nkRsSzflkm
     * @return
     */
    @AutoLog(value = "内控-收支分类科目-编辑")
    @ApiOperation(value = "内控-收支分类科目-编辑")
    //@RequiresPermissions("nk:NkRsSzflkm:edit")
    @PutMapping(value = "/edit")
    public HttpResult<?> edit(@RequestBody NkRsSzflkm nkRsSzflkm) {
        nkRsSzflkmService.updateById(nkRsSzflkm);
        return HttpResult.ok(I18nUtil.message("update.success"));
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "内控-收支分类科目-通过id删除")
    @ApiOperation(value = "内控-收支分类科目-通过id删除")
    //@RequiresPermissions("nk:NkRsSzflkm:delete")
    @DeleteMapping(value = "/delete")
    public HttpResult<?> delete(@RequestParam("id") String id) {
        nkRsSzflkmService.removeById(id);
        return HttpResult.ok(I18nUtil.message("delete.success"));
    }

    /**
     * 通过id批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "内控-收支分类科目-批量删除")
    @ApiOperation(value = "内控-收支分类科目-批量删除")
    //@RequiresPermissions("nk:NkRsSzflkm:delete")
    @DeleteMapping(value = "/deleteBatch")
    public HttpResult<?> deleteBatch(@RequestParam("ids") String ids) {
        nkRsSzflkmService.removeByIds(Arrays.asList(ids.split(",")));
        return HttpResult.ok(I18nUtil.message("delete.success"));
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "内控-收支分类科目-通过id查询")
    @ApiOperation(value = "内控-收支分类科目-通过id查询")
    @GetMapping(value = "/queryById")
    public HttpResult<?> queryById(@RequestParam("id") String id) {
        NkRsSzflkm nkRsSzflkm = nkRsSzflkmService.getById(id);
        return HttpResult.ok(nkRsSzflkm);
    }



    /**
     * 数据转换
     * @param nkRsSzflkm
     */
    private void formatValue(NkRsSzflkm nkRsSzflkm){
    }
}
